/**
 * @Title: DataValueTag.java   
 * @Copyright 2010 -2013 CreativeWise
 * @Package com.vmatrix.taglib   
 * @Description:    
 * @author wangxinmin    
 * @date 2014-1-13 上午11:41:04   
 * @version V1.0 
 */
package com.cwises.taglib;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts2.views.jsp.TagUtils;

import com.cwises.entity.system.user.UserInfoManager;
import com.opensymphony.xwork2.util.ValueStack;
import com.util.Util;

/**
 * @ClassName: DataValueTag
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author wangxinmin
 * @date 2014-1-13 上午11:41:04
 * 
 */
public class DataValueTag extends TagSupport {
	private static final long serialVersionUID = 0x28fb0bdeeab05ff3L;
	private String field;
	private String value;
	private String title;
	private String defaultValue;
	private String codeName;
	private String className;
	private String property;
	private String person;
	private String name;
	private String isValue;
	private String isCode;

	public DataValueTag() {

	}

	protected void doStart() throws JspException {
	}

	public int doEndTag() throws JspException {
		StringBuffer buf = new StringBuffer();
		String value = "";
		String name = "";
		String title = "";
		String rem = "";
		String dValue = getDefaultValue();
		String dataValue = "";
		if (getProperty() != null) {
			dataValue = (String) pageContext.getAttribute(getProperty());
			if (dataValue == null)
				dataValue = (String) pageContext.getRequest().getAttribute(
						getProperty());
		} else {
			dataValue = (String) pageContext.getAttribute("_DEAFULTDATA");
			if (dataValue == null)
				dataValue = (String) pageContext.getRequest().getAttribute(
						"_DEAFULTDATA");
		}
		if (dataValue == null) {
			ValueStack stack = TagUtils.getStack(pageContext);
			if (stack != null) {
				Object obj = stack.findValue(getField());
				if (obj != null) {
					dataValue = obj.toString();
				}
			}
		}
		if (dataValue != null && !"".equals(dataValue)) {
			if ("true".equalsIgnoreCase(getPerson())) {
				UserInfoManager userInfo = new UserInfoManager();
				try {
					userInfo = getUserInfo(dataValue);
				} catch (Exception e) {
					e.printStackTrace();
				}
				name = userInfo.getUserName();
				if (name == null) {
					name = (new StringBuilder("没有id为“")).append(dataValue).append("”的人员信息").toString();
				}
				title = name;
			}
		} else {
			name = dValue;
			title = name;
		}
		if (isValue == "true") {
			buf.append(Util.getString(name));
		} else {
			buf.append("<span ").append(" title=\"")
					.append(Util.getString(title)).append("\"")
					.append("  class=\"").append(getClassName()).append("\"")
					.append(">");
			buf.append(Util.getString(name));
			buf.append("</span >");

		}

		try {
			pageContext.getOut().print(buf.toString());
		} catch (IOException e) {
			throw new JspTagException((new StringBuilder("Select Tag ERROR:"))
					.append(e.getMessage()).toString());
		}
		return 6;
	}
	
	/**
	 * 
	* @Title: getUserInfo   
	* @Description:    根据用户id获取用户信息
	* @param @param userId
	* @param @return
	* @param @throws Exception    
	* @return UserInfoManager      
	* @author wangxinmin
	* @date 2014-2-28 下午1:33:28 
	* @throws
	 */
	public static UserInfoManager getUserInfo(String userId) throws Exception {
		Connection conn=null;
		UserInfoManager userInfo = new UserInfoManager();
		try {
			ResourceBundle p = ResourceBundle.getBundle("db");
			String driverClassName = p.getString("jdbc.driverClassName");
			String url = p.getString("jdbc.url");
			String jdbcusername = p.getString("jdbc.username");
			String jdbcpassword = p.getString("jdbc.password");
			Class.forName(driverClassName);
			conn=(Connection) DriverManager.getConnection(url,jdbcusername,jdbcpassword); 
			PreparedStatement pstmtCaption = null;	
			String sql="select * from	b_userinfomanager	WHERE	Status != '99' and UserId = '"+userId+"' limit 1" ;
			pstmtCaption=conn.prepareStatement(sql);
			
			ResultSet result =pstmtCaption.executeQuery();
			ResultSetMetaData md = result.getMetaData(); //得到结果集(rs)的结构信息，比如字段数、字段名等   
			while (result.next()) {
				userInfo.setUserName(result.getString("UserName"));
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally{
			if (conn != null) {
				try {
					if (!conn.isClosed()) {
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return userInfo;
	}

	/**
	 * @return the field
	 */
	public String getField() {
		return field;
	}

	/**
	 * @param field
	 *            the field to set
	 */
	public void setField(String field) {
		this.field = field;
	}

	/**
	 * @return the value
	 */
	public String getValue() {
		return value;
	}

	/**
	 * @param value
	 *            the value to set
	 */
	public void setValue(String value) {
		this.value = value;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the defaultValue
	 */
	public String getDefaultValue() {
		return defaultValue;
	}

	/**
	 * @param defaultValue
	 *            the defaultValue to set
	 */
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	/**
	 * @return the codeName
	 */
	public String getCodeName() {
		return codeName;
	}

	/**
	 * @param codeName
	 *            the codeName to set
	 */
	public void setCodeName(String codeName) {
		this.codeName = codeName;
	}

	/**
	 * @return the className
	 */
	public String getClassName() {
		return className;
	}

	/**
	 * @param className
	 *            the className to set
	 */
	public void setClassName(String className) {
		this.className = className;
	}

	/**
	 * @return the property
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property
	 *            the property to set
	 */
	public void setProperty(String property) {
		this.property = property;
	}

	/**
	 * @return the person
	 */
	public String getPerson() {
		return person;
	}

	/**
	 * @param person
	 *            the person to set
	 */
	public void setPerson(String person) {
		this.person = person;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the isValue
	 */
	public String getIsValue() {
		return isValue;
	}

	/**
	 * @param isValue
	 *            the isValue to set
	 */
	public void setIsValue(String isValue) {
		this.isValue = isValue;
	}

	/**
	 * @return the isCode
	 */
	public String getIsCode() {
		if (isCode == null || "null".equals(isCode)) {
			return "true";
		}
		return isCode;
	}

	/**
	 * @param isCode
	 *            the isCode to set
	 */
	public void setIsCode(String isCode) {
		this.isCode = isCode;
	}

}
